package com.example.Lezedata.ui.activity.address;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.Lezedata.R;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.BankCard.BankCardVcCell;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.util.GBUtil;

import java.util.ArrayList;

import bid.Bid;
import common.Common;

public class AddressListVc extends GBBaseVc {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bank_card_vc;
    }

    @Override
    public CharSequence title() {
        return "Bank Card";
    }

    ArrayList<Bid.ExpressAddress> models = new ArrayList<Bid.ExpressAddress>();
    private ListView tableView;
    BaseAdapter adapter;

    private View header;
    private TextView addButton;

    private int pageStart = 0;
    private int pageSize = 20;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        tableView = findViewById(R.id.tableView);
        addButton = findViewByTag("add");

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpActivity(Address_addVc.class);
            }
        });

        addDiverLine();
        setAdapter();
        loadData();
    }
    @RequiresApi(api = Build.VERSION_CODES.M)
    TextView addDiverLine(){
        TextView line = new TextView(mContext);
        line.setHeight(GBUtil.dpTo2px(mContext,10));
        line.setBackground(new ColorDrawable(getColor(R.color.app_color_inputBackground)));
        tableView.addHeaderView(line);
        return line;
    }

    void addHeader(){
        header = LayoutInflater.from(mContext).inflate (R.layout.bank_card_no_data,null,false);
        TextView textView = header.findViewWithTag("text");
        textView.setText("Please add your address");
        tableView.addHeaderView(header);
    }

    void loadData(){
        hudShow();
//        GBNetRequest.stubUser().getExpressAddressList(Common.PageInfo.newBuilder().setPageSize(pageSize).setPageStart(pageStart).build(),
//                ServerResponseObserver.observer(new ServerResponseObserver.requestListener<Bid.GetExpressAddressListResponse>() {
//            @Override
//            public void complete(Bid.GetExpressAddressListResponse value, GBError error) {
//                hudDismiss();
//                if (error == null){
//                    if (value.getListCount() == pageSize){
//                        pageSize ++;
//                    }
//                    models.addAll(value.getListList());
//                    adapter.notifyDataSetChanged();
//                }
//            }
//        }));
    }

    void setAdapter(){
        adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                int count = models.size();
                if (count > 0){
                    tableView.removeHeaderView(header);
                    header = null;
                }else {
                    if (header == null){
                        addHeader();
                    }
                }
                return count;
            }

            @Override
            public Bid.ExpressAddress getItem(int position) {
                return models.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null){
                    convertView = AddressListVcCell.newInstance(mContext);
                }
                AddressListVcCell cell = (AddressListVcCell)convertView;
                cell.listener = new AddressListVcCell.ClassListener() {
                    @Override
                    public void delete(Bid.ExpressAddress expressAddress) {
                        deleteExpressAddress(expressAddress);
                    }

                    @Override
                    public void edit(Bid.ExpressAddress expressAddress) {
                        editExpressAddress(expressAddress);
                    }
                };
                cell.setExpressAddress(getItem(position));
                return convertView;
            }
        };
        tableView.setAdapter(adapter);
    }

    private void deleteExpressAddress(Bid.ExpressAddress expressAddress) {

    }

    private void editExpressAddress(Bid.ExpressAddress expressAddress) {

    }
}